package com.example.springdata.service.book;

import com.example.productdataserviceprovider.service.AbstractService;
import com.example.springdata.dao.entity.Book;
import com.example.springdata.dao.entity.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class BookServiceImpl extends AbstractService implements BookService {
    @Autowired
    private BookRepository bookRepository;

    @Override
    public void generateData(int total) {
        List<Book> books=new ArrayList<>();
        for(int i=1;i<=total;i++){
            if(i%10000==0){
                logger.info("执行到：{}",i);
//                System.out.println("执行到：{}"+i);
                this.bookRepository.saveAll(books);
                books=new ArrayList<>();
            }
            LocalDateTime time= LocalDateTime.now();
            Book book=new Book();
            book.setName("wangyi"+i);
            book.setPrice(100+i);
            book.setCreateDate(time);
            book.setPublishDate(LocalDate.now());
            book.setPublisher("出版商"+i);
            books.add(book);
        }
        this.bookRepository.saveAll(books);

    }

    @Override
    public Page<Book> findByName(String name) {
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC,"createDate");
        Page<Book> page = bookRepository.findProduct("bauer",pageable);
        return page;
    }


    @Override
    public void save(Book product) {
        this.bookRepository.save(product);
    }
}
